#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "ClientEngine.h"
#include <QValidator>
#include "ErrorMessage.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

	QRegExp regExpIp("(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])");
	QValidator* qvalIp = new QRegExpValidator(regExpIp, this);
	ui->leIp->setValidator(qvalIp);

	QIntValidator* validPort = new QIntValidator (0, 24000, this );
    ui->lePort->setValidator( validPort );

	newClientEngine.create();

	// ******************************************************************************************************************
	// Set device lists of 3 comboboxes
	// ******************************************************************************************************************
	QStringList qRecordingDevicesNameList = newClientEngine.GetRecordingDeviceList();
	QStringList qAudioDevicesNameList = newClientEngine.GetAudioDeviceList();
	QStringList qCaptureDevicesNameList = newClientEngine.GetVideoDeviceList();

	ui->cbOutput->addItems(qAudioDevicesNameList);
	ui->cbInput->addItems(qRecordingDevicesNameList);
	
	ui->hsPlayout->setValue(newClientEngine.getPlayoutVolume());
	ui->hsRecording->setValue(newClientEngine.getMicVolume());

	if (qCaptureDevicesNameList[0] == EMPTY)
	{
		ui->cbVideo->setEnabled(false);
		ui->pbTestVideo->setEnabled(false);
	}
	else ui->cbVideo->addItems(qCaptureDevicesNameList);

	ui->leLocalIp->setText(newClientEngine.getLocalIp());

	inACall = false;
	test = false;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
	if (!inACall)
	{
		QString qStrPort = ui->lePort->displayText();
		QString qStrIp = ui->leIp->displayText();

		HWND localVideoWindowHWND;
		HWND remoteVideoWindowHWND;

		if (newClientEngine.getVideoState())
		{
			localVideoWindow=new QMainWindow();
			localVideoWindow->setFixedSize(320, 240);
			localVideoWindow->setWindowTitle( "Local" );

			localVideoWindowHWND = localVideoWindow->winId();

			remoteVideoWindow=new QMainWindow();

			remoteVideoWindow->setFixedSize(640, 480);
			remoteVideoWindow->setWindowTitle( "Remote" );

			remoteVideoWindowHWND = remoteVideoWindow->winId();

			// ******************************************************************************************************************
			// At first we show form, because it's very important. It's related at features of Gips/WebRTC
			// ******************************************************************************************************************
			remoteVideoWindow->show();
			localVideoWindow->show();
		}
			if (newClientEngine.setConnection(qStrPort, qStrIp, localVideoWindowHWND, remoteVideoWindowHWND) != -1) 
			{
				inACall = true;
		
				ui->pushButton->setText("Cancel");
		
				ui->leIp->setEnabled(false);
		
				ui->lePort->setEnabled(false);
		
				ui->cbInput->setEnabled(false);
		
				ui->cbOutput->setEnabled(false);
		
				ui->cbVideo->setEnabled(false);
			}
			else
			{
				ERROR_MESSAGE("Can't make the call! Aborting...");
				if (newClientEngine.getVideoState())
				{
					delete remoteVideoWindow;
					delete localVideoWindow;
				}
			}
	}
	else
	{
		inACall = false;
		
		ui->pushButton->setText("Call");
		
		ui->leIp->setEnabled(true);
		
		ui->lePort->setEnabled(true);
		
		ui->cbInput->setEnabled(true);
		
		ui->cbOutput->setEnabled(true);
		
		ui->cbVideo->setEnabled(true);
		
		newClientEngine.endConnection();
		
		delete remoteVideoWindow;
		delete localVideoWindow;
	}
}

void MainWindow::on_pbTestVideo_clicked()
{
	// ******************************************************************************************************************
	// Simple test for active webCam
	// ******************************************************************************************************************
	if (!test)
	{
		QMainWindow *localVideoWindow=new QMainWindow();
		localVideoWindow->setFixedSize(640, 480);
		localVideoWindow->setWindowTitle( "Test capture" );

		HWND localVideoWindowHWND = localVideoWindow->winId();

		localVideoWindow->show();
		ui->pbTestVideo->setText("Cancel");
		newClientEngine.testWebcam(localVideoWindowHWND);
		test = true;
	}
	else
	{
		newClientEngine.endTestWebcam();
		delete localVideoWindow;
		ui->pbTestVideo->setText("Test video");
		test = false;
	}
}

void MainWindow::on_cbVideo_currentIndexChanged(int index)
{
	// ******************************************************************************************************************
	// When combobox active value is changed set the corresponding variable at current index
	// ******************************************************************************************************************
	int currentIndex = ui->cbVideo->currentIndex();
	newClientEngine.setRecordingDevice(currentIndex);
}

void MainWindow::on_cbInput_currentIndexChanged(int index)
{
	// ******************************************************************************************************************
	// When combobox active value is changed set the corresponding variable at current index
	// ******************************************************************************************************************
	int currentIndex = ui->cbInput->currentIndex();
	newClientEngine.setCaptureDevice(currentIndex);
}

void MainWindow::on_cbOutput_currentIndexChanged(int index)
{
	// ******************************************************************************************************************
	// When combobox active value is changed set the corresponding variable at current index
	// ******************************************************************************************************************
	int currentIndex = ui->cbOutput->currentIndex();
	newClientEngine.setPlayoutDevice(currentIndex);
}

//void MainWindow::on_MainWindow_destroyed()
//{

//}


void MainWindow::on_hsPlayout_valueChanged(int value)
{
	newClientEngine.setPlayoutVolume(value);
}

void MainWindow::on_hsRecording_valueChanged(int value)
{
	newClientEngine.setMicVolume(value);
}

